//
//  adScrollView.m
//  Mdongying
//
//  Created by 伟帅陈 on 13-11-19.
//  Copyright (c) 2013年 伟帅陈. All rights reserved.
//

#import "adScrollView.h"
#import "AppUtility.h"
#import "UIImageView+WebCache.h"

@implementation adScrollView

@synthesize imageArray;
@synthesize typeArray;
@synthesize ADelegate;
@synthesize titleArrays;
@synthesize pageCurrent;

- (id)initWithFrame:(CGRect)frame ImageArrays:(NSMutableArray*)imageArrays typeArrays:(NSMutableArray *)typeArrays titleDictionary:(NSMutableArray *)titleDictionAry pageConFrame:(CGRect )pageConFame isTime:(BOOL)isTime PageColor:(UIColor *)PageColor
{
    self = [super initWithFrame:frame];
    if (self) {
        with=self.frame.size.width;
        num = 0;
        self.userInteractionEnabled = YES;
        self.scrollEnabled = YES;
        self.backgroundColor =[UIColor clearColor];
        self.typeArray = [NSMutableArray arrayWithArray:typeArrays];
        if (imageArrays==nil) {
        
            self.imageArray = [NSMutableArray arrayWithCapacity:self.typeArray.count];
            for (int i =0; i<self.typeArray.count; i++) {
                [self.imageArray addObject:[AppUtility getLocalImage:@"home_touming"]];
            }
            self.titleArrays =[NSMutableArray arrayWithArray:titleDictionAry];
            
        }
        else
        {
            self.imageArray =[NSMutableArray arrayWithArray:imageArrays];

        }

        scrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        scrollview.userInteractionEnabled = YES;
        scrollview.scrollEnabled = YES;
        scrollview.delegate = self;
        scrollview.pagingEnabled = YES;
        scrollview.showsHorizontalScrollIndicator = NO;
        scrollview.contentSize=CGSizeMake(self.frame.size.width *self.imageArray .count, self.frame.size.height);
        scrollview.backgroundColor = [UIColor clearColor];
        [self addSubview:scrollview];
        
        pageC = [[UIPageControl alloc]initWithFrame:pageConFame];
        pageC.numberOfPages =[self.imageArray  count];
        pageC.hidesForSinglePage = YES;
        pageC.pageIndicatorTintColor = RGBCOLOR(166, 166, 166);
        pageC.currentPageIndicatorTintColor = PageColor;
        pageC.currentPage = 0;
        for (int i = 0; i<[self.imageArray  count]; i++) {
            
            UIButton *btn =[UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame =CGRectMake(self.frame.size.width *i,0, self.frame.size.width, self.frame.size.height);
            btn.backgroundColor = [UIColor clearColor];
            

            
            

            UIImageView *imageV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, btn.frame.size.width, btn.frame.size.height)];
            imageV.backgroundColor = [UIColor clearColor];
            [btn addSubview:imageV];
//            [imageV release];
            
            [imageV sd_setImageWithURL:[NSURL URLWithString:[self.imageArray  objectAtIndex:i]]  placeholderImage:[AppUtility getLocalJGPImage:@"placehoder_gray"]];
            imageV.clipsToBounds = YES;

            imageV.contentMode = UIViewContentModeScaleAspectFill;

            [btn addTarget:self action:@selector(pushto:) forControlEvents:UIControlEventTouchUpInside];
            btn.tag=[[self.typeArray objectAtIndex:i] intValue];
            [scrollview addSubview:btn];
            
            if (titleDictionAry!=nil) {
                UILabel *titleLabel =[[UILabel alloc]initWithFrame:CGRectMake(0, 15, with, 20)];
                titleLabel.text = [[self.titleArrays objectAtIndex:i] objectForKey:@"title"];
                [titleLabel setFont:[UIFont boldSystemFontOfSize:18]];
                titleLabel.textColor =[UIColor blackColor];
                titleLabel.backgroundColor = [UIColor clearColor];
                [btn addSubview:titleLabel];
//                [titleLabel release];
                
                UILabel *smallLabel =[[UILabel alloc]initWithFrame:CGRectMake(0, 45, with, 15)];
                smallLabel.text = [[self.titleArrays objectAtIndex:i] objectForKey:@"uptime"];
                [smallLabel setFont:[UIFont systemFontOfSize:14]];
                smallLabel.textColor =[UIColor grayColor];
                smallLabel.backgroundColor = [UIColor clearColor];
                [btn addSubview:smallLabel];
//                [smallLabel release];
            }

        }
        [self addSubview:pageC];
//        [pageC release];
        
        if (isTime) {
            [NSTimer scheduledTimerWithTimeInterval:4 target: self selector: @selector(changeImageV)  userInfo:nil  repeats: YES];
        }
        
    }
    return self;
}

-(void)pushto:(UIButton *)btn
{
    [self.ADelegate viewTappedwithAdScrollView:self Type:btn.tag];
}

-(void)changeImageV
{
    if (scrollview.contentOffset.x == (self.imageArray.count-1 )*with)
    {
        [scrollview setContentOffset:CGPointMake(0, 0) animated:YES];
    }
    else
    {
        [scrollview setContentOffset:CGPointMake(scrollview.contentOffset.x+with, 0) animated:YES];

    }

}


-(void)adScrollViewChangeImageArrys:(NSMutableArray *)Up_imageArray TypeArrays:(NSMutableArray *)Up_typeArrays titleArrays:(NSMutableArray *)Up_titleArrays;

{
    for (UIView*subview in scrollview.subviews) {
        [subview removeFromSuperview];
    }
    
    if (Up_imageArray!=nil) {
        
        [self.imageArray removeAllObjects];
        self.imageArray = [NSMutableArray arrayWithArray:Up_imageArray];

        for (int i = 0; i<[Up_imageArray  count]; i++) {
            
            UIButton *btn =[UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame =CGRectMake(self.frame.size.width *i,0, self.frame.size.width, self.frame.size.height);
//            [btn setImage:[Up_imageArray  objectAtIndex:i] forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(pushto:) forControlEvents:UIControlEventTouchUpInside];
            btn.tag=[[Up_typeArrays objectAtIndex:i] intValue];
            [scrollview addSubview:btn];
            
            ;
            UIImageView *imageV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, btn.frame.size.width, btn.frame.size.height)];
            [btn addSubview:imageV];
//            [imageV release];
            
            
//            [imageV setImage:[AppUtility getLocalImage:self.imageArray[i]]];
            [imageV sd_setImageWithURL:[NSURL URLWithString:[self.imageArray  objectAtIndex:i]]  placeholderImage:[AppUtility getLocalImage:@"home_oneIcon_default"]];
            imageV.clipsToBounds = YES;
            imageV.contentMode = UIViewContentModeScaleAspectFill;
        }
    }
    else
    {
        [self.typeArray removeAllObjects];
        self.typeArray = [NSMutableArray arrayWithArray:Up_typeArrays];
        
        [self.imageArray removeAllObjects];
        self.imageArray = [NSMutableArray arrayWithCapacity:self.typeArray.count];
        for (int i =0; i<self.typeArray.count; i++) {
            
                [self.imageArray addObject:[AppUtility getLocalImage:@"home_touming"]];
            }
        
        
        self.titleArrays =[NSMutableArray arrayWithArray:Up_titleArrays];

        for (int i = 0; i<[self.typeArray  count]; i++) {
            
            UIButton *btn =[UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame =CGRectMake(self.frame.size.width *i,0, self.frame.size.width, self.frame.size.height);
            btn.backgroundColor = [UIColor clearColor];
            [btn addTarget:self action:@selector(pushto:) forControlEvents:UIControlEventTouchUpInside];
            btn.tag=[[self.typeArray objectAtIndex:i] intValue];
            [scrollview addSubview:btn];
            
            
            if (Up_titleArrays!=nil) {
                UILabel *titleLabel =[[UILabel alloc]initWithFrame:CGRectMake(0, 10, with, 20)];
                titleLabel.text = [[self.titleArrays objectAtIndex:i] objectForKey:@"title"];
                [titleLabel setFont:[UIFont fontWithName:nil size:18]];
                titleLabel.textColor =[UIColor blackColor];
                titleLabel.backgroundColor = [UIColor clearColor];
                [btn addSubview:titleLabel];
//                [titleLabel release];
                
                UILabel *smallLabel =[[UILabel alloc]initWithFrame:CGRectMake(0, 45, with, 15)];
                smallLabel.text = [[self.titleArrays objectAtIndex:i] objectForKey:@"uptime"];
                [smallLabel setFont:[UIFont systemFontOfSize:14]];
                smallLabel.textColor =[UIColor grayColor];
                smallLabel.backgroundColor = [UIColor clearColor];
                [btn addSubview:smallLabel];
//                [smallLabel release];

                
            }

        }

    }
    
    
    scrollview.contentSize=CGSizeMake(self.frame.size.width *Up_typeArrays.count, self.frame.size.height);

    pageC.numberOfPages =[Up_typeArrays  count];

}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    pageC.currentPage = scrollView.contentOffset.x/scrollView.frame.size.width;
    self.pageCurrent = (int)pageC.currentPage;
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    pageC.currentPage = scrollView.contentOffset.x/scrollView.frame.size.width;
    self.pageCurrent = (int)pageC.currentPage;

}



@end
